package com.chen.servicemap.remote;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.chen.internalcommon.constant.MapConfigConstants;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.internalcommon.response.TerminalAroundSearchResponse;
import com.chen.internalcommon.response.TerminalResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenLiuPing
 **/
@Service
@Slf4j
public class TerminalClient {

    @Value("${map.key}")
    private String key;

    @Value(("${map.sid}"))
    private String sid;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult add(String name, String desc) {
        log.info("name:{}", name);
        // 拼接url
        StringBuilder url = new StringBuilder(MapConfigConstants.TERMINAL_ADD);
        url.append("?key=").append(key);
        url.append("&sid=").append(sid);
        url.append("&name=").append(name);
        url.append("&desc=").append(desc);
        log.info("add ===url:{}", url);
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);

        JSONObject data = jsonObject.getJSONObject("data");
        String tid = data.getString("tid");
        TerminalResponse terminalResponse = new TerminalResponse(tid);
        return ResponseResult.success(terminalResponse);
    }

    public ResponseResult aroundSearch(String center, Integer radius) {
        log.info("aroundSearch start");
        // 拼接url
        StringBuilder url = new StringBuilder(MapConfigConstants.TERMINAL_AROUND_SEARCH);
        url.append("?key=").append(key);
        url.append("&sid=").append(sid);
        url.append("&center=").append(center);
        url.append("&radius=").append(radius);
        log.info("aroundSearch === url:{}", url);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        String errmsg = jsonObject.getString("errmsg");
        if ("OK".equals(errmsg)) {
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray results = data.getJSONArray("results");
            List<TerminalAroundSearchResponse> list = new ArrayList<>();
            for (int i = 0; i < results.size(); i++) {
                JSONObject result = results.getJSONObject(i);
                Long carId = result.getLong("desc");
                String tid = result.getString("tid");
                JSONObject location = result.getJSONObject("location");
                String latitude = location.getString("latitude");
                String longitude = location.getString("longitude");
                list.add(new TerminalAroundSearchResponse(tid, carId, latitude, longitude));
            }
            log.info("搜索成功，查询到{}辆车", list.size());
            return ResponseResult.success(list);
        }
        String errdetail = jsonObject.getString("errdetail");
        log.info("搜索失败：{}", errdetail);
        return ResponseResult.fail(errdetail);
    }
}
